home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 3 / Gold Medal Software - Volume 3 (Gold Medal) (1994).iso / bbsutils / smb_110a.arj / SYNC2SMB.C < prev    next >
C/C++ Source or Header  |  1994-03-22  |  7KB  |  290 lines

  1. /* SYNC2SMB.C */
  2.  
  3. /* Converts Synchronet Version 1 (proprietary) message base to SMB format */
  4.  
  5. /* The intention of this source code is an example of how to use the SMBLIB */
  6. /* library functions to access an SMB format message base.                    */
  7.  
  8. /* This program and source code are freeware. May be used in part or whole    */
  9. /* for any purpose without consent or notification of Digital Dynamics.     */
  10.  
  11. /* Digital Dynamics does request that developers that release products that */
  12. /* support the SMB format notify Digital Dynamics so the implementation     */
  13. /* and contact chapters in the technical specification may be updated.        */
  14.  
  15. #include "smblib.h"
  16. #include "crc32.h"
  17.  
  18. char sbbs[128];
  19.  
  20. /****************************************************************************/
  21. /* Updates 16-bit "rcrc" with character 'ch'                                */
  22. /****************************************************************************/
  23. void ucrc16(uchar ch, ushort *rcrc) {
  24.     ushort i, cy;
  25.     uchar nch=ch;
  26.  
  27. for (i=0; i<8; i++) {
  28.     cy=*rcrc & 0x8000;
  29.     *rcrc<<=1;
  30.     if (nch & 0x80) *rcrc |= 1;
  31.     nch<<=1;
  32.     if (cy) *rcrc ^= 0x1021; }
  33. }
  34.  
  35. /****************************************************************************/
  36. /* Returns 16-crc of string (not counting terminating NULL)                 */
  37. /****************************************************************************/
  38. ushort crc16(char *str)
  39. {
  40.     int     i=0;
  41.     ushort    crc=0;
  42.  
  43. ucrc16(0,&crc);
  44. while(str[i])
  45.     ucrc16(str[i++],&crc);
  46. ucrc16(0,&crc);
  47. ucrc16(0,&crc);
  48. return(crc);
  49. }
  50.  
  51.  
  52. /****************************************************************************/
  53. /* Truncates white-space chars off end of 'str' and terminates at first tab */
  54. /****************************************************************************/
  55. void truncsp(char *str)
  56. {
  57.     char c;
  58.  
  59. str[strcspn(str,"\t")]=0;
  60. c=strlen(str);
  61. while(c && (uchar)str[c-1]<=' ') c--;
  62. str[c]=0;
  63. }
  64.  
  65. /****************************************************************************/
  66. /* Converts an ASCII Hex string into an ulong                       */
  67. /****************************************************************************/
  68. ulong ahtoul(char *str)
  69. {
  70.     ulong l,val=0;
  71.  
  72. while((l=(*str++)|0x20)!=0x20)
  73.     val=(l&0xf)+(l>>6&1)*9+val*16;
  74. return(val);
  75. }
  76.  
  77. /****************************************************************************/
  78. /* Returns the length of the file in 'filespec'                             */
  79. /****************************************************************************/
  80. long flength(char *filespec)
  81. {
  82.     struct ffblk f;
  83.  
  84. if(findfirst(filespec,&f,0)==0)
  85.     return(f.ff_fsize);
  86. return(-1L);
  87. }
  88.  
  89. /****************************************************************************/
  90. /* Returns the FidoNet address kept in str as ASCII.                        */
  91. /****************************************************************************/
  92. fidoaddr_t atofaddr(char *str)
  93. {
  94.     char *p;
  95.     fidoaddr_t addr;
  96.  
  97. addr.zone=addr.net=addr.node=addr.point=0;
  98. if((p=strchr(str,':'))!=NULL) {
  99.     addr.zone=atoi(str);
  100.     addr.net=atoi(p+1); }
  101. else {
  102.     addr.zone=1;
  103.     addr.net=atoi(str); }
  104. if(!addr.zone)                /* no such thing as zone 0 */
  105.     addr.zone=1;
  106. if((p=strchr(str,'/'))!=NULL)
  107.     addr.node=atoi(p+1);
  108. else {
  109.     addr.net=1;
  110.     addr.node=atoi(str); }
  111. if((p=strchr(str,'.'))!=NULL)
  112.     addr.point=atoi(p+1);
  113. return(addr);
  114. }
  115.  
  116.  
  117. int main(int argc, char **argv)
  118. {
  119.     uchar    *p,str[128],from[128],to[128],subj[128],fname[128]
  120.             ,buf[SDT_BLOCK_LEN];
  121.     ushort    ftype,flen,xlat;
  122.     ushort    i,j,k,x,msgs,net;
  123.     ulong    length,crc;
  124.     FILE    *ixt,*stream;
  125.     smbmsg_t    msg;
  126.     smbstatus_t status;
  127.     fidoaddr_t    faddr;
  128.  
  129. if(argc<2) {
  130.     printf("usage: sync2smb <sub_code>\r\n");
  131.     exit(1); }
  132.  
  133. p=getenv("SBBS");
  134. if(!p) {
  135.     printf("SBBS environment variable must be set.\r\n");
  136.     printf("example: SET SBBS=C:\\SBBS\r\n");
  137.     exit(1); }
  138.  
  139. strcpy(sbbs,p);
  140. strupr(sbbs);
  141. strcpy(smb_file,argv[1]);
  142. strupr(smb_file);
  143.  
  144. smb_open(10);
  145.  
  146. sprintf(str,"%s\\INDX\\SUBS\\%s.IXT",sbbs,smb_file);
  147. if((ixt=fopen(str,"rb"))==NULL) {
  148.     printf("error opening %s\r\n",str);
  149.     exit(1); }
  150.  
  151. if(!filelength(fileno(shd_fp)))
  152.     smb_create(2000,2000,0,10);
  153.  
  154. fread(str,1,17,ixt);
  155. msgs=atoi(str);
  156. smb_getstatus(&status);       // Initialized for first call to smb_addcrc()
  157.  
  158. for(i=0;i<msgs;i++) {
  159.     printf("%4u of %-4u\r",i+1,msgs);
  160.     memset(&msg,0,sizeof(smbmsg_t));
  161.     memcpy(msg.hdr.id,"SHD\x1a",4);
  162.     msg.hdr.version=SMB_VERSION;
  163.  
  164.  
  165.     if(!fgets(str,81,ixt))
  166.         break;
  167.     str[8]-=SP;
  168.     if(str[8]&(1<<0))
  169.         msg.idx.attr|=MSG_PERMANENT;
  170.     if(str[8]&(1<<1))
  171.         msg.idx.attr|=MSG_ANONYMOUS;
  172.     if(str[8]&(1<<2))
  173.         msg.idx.attr|=MSG_PRIVATE;
  174.     msg.hdr.attr=msg.idx.attr;
  175.     str[8]=0;
  176.     strcpy(fname,str);
  177.     str[17]=0;
  178.     net=NET_NONE;
  179.  
  180.  
  181.     if(toupper(str[0])=='F')
  182.         net=NET_FIDO;
  183.     else if(toupper(str[0])=='Q')
  184.         net=NET_QWK;
  185.     else if(toupper(str[0])=='P')
  186.         net=NET_POSTLINK;
  187.     msg.hdr.when_written.time=ahtoul(str+9);
  188.     strcpy(from,str+18);
  189.     truncsp(from);
  190.     p=strrchr(from,'@');
  191.     if(p) {
  192.         *p=0;
  193.         truncsp(from);
  194.         p++;
  195.         if(net==NET_FIDO) {
  196.             faddr=atofaddr(p);
  197.             smb_hfield(&msg,SENDERNETADDR,sizeof(fidoaddr_t),&faddr); }
  198.         else if(net)
  199.             smb_hfield(&msg,SENDERNETADDR,strlen(p),p); }
  200.  
  201.     if(net)
  202.         smb_hfield(&msg,SENDERNETTYPE,sizeof(ushort),&net);
  203.     smb_hfield(&msg,SENDER,strlen(from),from);
  204.     strlwr(from);
  205.     msg.idx.from=crc16(from);
  206.  
  207.  
  208.     if(!fgets(str,81,ixt))
  209.         break;
  210.     str[8]=0;
  211.     msg.hdr.when_imported.time=ahtoul(str);
  212.     strcpy(to,str+9);
  213.     truncsp(to);
  214.     smb_hfield(&msg,RECIPIENT,strlen(to),to);
  215.     strlwr(to);
  216.     msg.idx.to=crc16(to);
  217.  
  218.     if(!fgets(subj,81,ixt))
  219.         break;
  220.     truncsp(subj);
  221.     smb_hfield(&msg,SUBJECT,strlen(subj),subj);
  222.     strlwr(subj);
  223.     msg.idx.subj=crc16(subj);
  224.  
  225.     sprintf(str,"%s\\DATA\\SUBS\\%s\\%s.MSG",sbbs,smb_file,fname);
  226.     length=flength(str);
  227.     if(length<1L) {
  228.         printf("invalid length (%ld) for %s\n",length,str);
  229.         exit(1); }
  230.     if((stream=fopen(str,"rb"))==NULL) {
  231.         printf("Error opening %s for read\n",str);
  232.         exit(1); }
  233.     fgets(str,81,stream);
  234.     length-=strlen(str);
  235.     fgets(str,81,stream);
  236.     length-=strlen(str);
  237.     fgets(str,81,stream);
  238.     length-=strlen(str);
  239.     fgets(str,81,stream);
  240.     length-=strlen(str);
  241.  
  242.     length+=2;    /* for translation string */
  243.  
  244.     if(smb_open_da(10)) {
  245.         printf("error opening %s.SDA\n",smb_file);
  246.         exit(1); }
  247.     msg.hdr.offset=smb_fallocdat(length,1);
  248.     fclose(sda_fp);
  249.     if(msg.hdr.offset && msg.hdr.offset<1L) {
  250.         printf("error %ld allocating blocks\r\n",msg.hdr.offset);
  251.         exit(1); }
  252.     fseek(sdt_fp,msg.hdr.offset,SEEK_SET);
  253.     xlat=XLAT_NONE;
  254.     fwrite(&xlat,2,1,sdt_fp);
  255.     crc=0xffffffffUL;
  256.     x=SDT_BLOCK_LEN-2;
  257.     while(!feof(stream)) {
  258.         memset(buf,NULL,x);
  259.         k=fread(buf,1,x,stream);
  260.         while(k && !buf[k-1]) k--;    /* take off NULLs, if any */
  261.         if((feof(stream) || k!=x) && buf[k-1]==LF && buf[k-2]==CR)
  262.             k-=2;
  263.         for(j=0;j<k;j++)
  264.             crc=ucrc32(buf[j],crc);
  265.         fwrite(buf,k,1,sdt_fp);
  266.         x=SDT_BLOCK_LEN; }
  267.     fclose(stream);
  268.  
  269.     crc=~crc;
  270.  
  271.  
  272.     j=smb_addcrc(status.max_crcs,crc,10);
  273.     if(j) {
  274.         if(j==1) {
  275.             printf("\nDuplicate message\n");
  276.             smb_freemsgmem(msg);
  277.             continue; }
  278.         printf("smb_addcrc returned %d\n",j);
  279.         exit(1); }
  280.  
  281.  
  282.     smb_dfield(&msg,TEXT_BODY,length);
  283.  
  284.     smb_addmsghdr(&msg,&status,1,10);
  285.     smb_freemsgmem(msg); }
  286.  
  287. return(0);
  288. }
  289.  
  290.